package com.yiwei.ydd.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.ydd.R;
import com.yiwei.ydd.api.model.RechargeInstantModel;
import com.yiwei.ydd.util.Dialog;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class RechargeTypeAdapter extends ItemAdapter<RechargeInstantModel.DatasBean.ItemBean, RechargeTypeAdapter.DefaultHolder> {

    private Context context;

    public RechargeTypeAdapter(Context context) {
        super(context);
        this.context = context;
    }

    public String saleNum;

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_recharge_type, parent));
    }

    public void setSaleNum(String saleNum) {
        this.saleNum = saleNum;
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        RechargeInstantModel.DatasBean.ItemBean item = getItem(position);

        if (Float.valueOf(item.money)==0) {
            holder.layoutTotal.setBackgroundResource(R.drawable.bg_money_unselect);
            holder.txtMoney.setTextColor(0xff000000);
            holder.txtMoneySec.setVisibility(View.GONE);
            holder.txtMoney.setText("+ 自定义");
        } else {
            holder.txtMoneySec.setVisibility(View.VISIBLE);
            if (Integer.valueOf(item.inventory) <= 0) {
//                如果库存为0
                holder.txtMoneySec.setText("已售馨");
                holder.layoutTotal.setBackgroundResource(R.drawable.bg_money_unselect);
                holder.txtMoney.setTextColor(0xffCCCCCC);
                holder.txtMoneySec.setTextColor(0xffCCCCCC);
            } else {
                if (item.isCheck) {
                    if (item.recommend.equals("0")) {
                        holder.layoutTotal.setBackgroundResource(R.mipmap.img_money_select);
                        holder.txtMoney.setTextColor(0xffFB5E01);
                        holder.txtMoneySec.setTextColor(0xffFB5E01);
                    } else {
                        holder.layoutTotal.setBackgroundResource(R.mipmap.img_oil_sign_bg_check);
                        holder.txtMoney.setTextColor(0xffffffff);
                        holder.txtMoneySec.setTextColor(0xffffffff);
                    }


                } else {
                    if (item.recommend.equals("0")) {
                        holder.layoutTotal.setBackgroundResource(R.drawable.bg_money_unselect);
                        holder.txtMoney.setTextColor(0xff000000);
                        holder.txtMoneySec.setTextColor(0xff666666);
                    } else {
                        holder.layoutTotal.setBackgroundResource(R.mipmap.img_oil_sign_bg);
                        holder.txtMoney.setTextColor(0xffffffff);
                        holder.txtMoneySec.setTextColor(0xffffffff);
                    }

                }

//            holder.txtMoneySec.setText("售价" + item.sale_money + "元");
                holder.txtMoneySec.setText(saleNum + "折");
            }

            holder.txtMoney.setText(item.money.replace(".00", "").replace(".0", "") + "元");


        }


        holder.layoutTotal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Integer.valueOf(item.inventory) <= 0) {
                    if (Float.valueOf(item.money) == 0||position == getItemCount() - 1) {
                        Dialog.showDefaultDialog(context, "", "自定义输入功能暂时关闭请过段时间再来试试吧", "", "好的，我知道了", false, new Dialog.DialogDefaultClickListener() {
                            @Override
                            public void confirm() {

                            }

                            @Override
                            public void cancel() {

                            }
                        });
                    }else {
                        Dialog.showDefaultDialog(context, "", "该充值面额已售馨，请明天再来购买吧", "", "好的，我知道了", false, new Dialog.DialogDefaultClickListener() {
                            @Override
                            public void confirm() {

                            }

                            @Override
                            public void cancel() {

                            }
                        });
                    }
                    return;
                }






                for (int i = 0; i < getItemCount(); i++) {
                    if (i == position) {
                        getmSource().get(i).isCheck = true;
                    } else {
                        getmSource().get(i).isCheck = false;
                    }
                    notifyDataSetChanged();
                }
                if (onChooseListener != null) {
                    onChooseListener.onClick(position, item.money, item.sale_money);
                }
            }
        });
    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_money)
        TextView txtMoney;
        @BindView(R.id.txt_money_sec)
        TextView txtMoneySec;
        @BindView(R.id.layout_total)
        LinearLayout layoutTotal;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onClick(int poi, String money, String sale_money);
    }
}
